<!DOCTYPE html>
<html lang="zh" xmlns:th="http://www.thymeleaf.org">
<head>
    <th:block th:include="include :: header('新增资讯')"/>
    <th:block th:include="include :: bootstrap-fileinput-css"/>
    <th:block th:include="include :: summernote-css"/>
</head>
<body class="white-bg">
<div class="wrapper wrapper-content animated fadeInRight ibox-content">
    <form class="form-horizontal m" id="form-news-add">
        <div class="row">
            <div class="col-sm-12">
                <div class="form-group">
                    <label class="col-sm-2 control-label">资讯标题：</label>
                    <div class="col-sm-9">
                        <input name="newTitle" class="form-control" type="text" required>
                    </div>
                </div>
            </div>
        </div>

        <div class="row">
            <div class="col-sm-12">
                <div class="form-group">
                    <label class="col-sm-2 control-label">封面图：</label>
                    <div class="col-sm-9 layui-upload">
                        <div class="layui-upload-list">
                            <input id="newPic" name="newPic" class="form-control" type="hidden">
                            <img class="layui-upload-img" id="pic" style="width: 220px;height: 180px;">
                        </div>
                        <button type="button" class="layui-btn" id="addBtn">上传图片</button>
                    </div>
                </div>

            </div>
        </div>

        <div class="row">
            <div class="col-sm-6">
                <div class="form-group">
                    <label class="col-sm-4 control-label">资讯分类：</label>
                    <div class="col-sm-8">
                        <select name="newTypeId" class="form-control m-b">
                            <option th:each="type:${typeList}" th:text="${type.typeName}" th:value="${type.typeId}"/>
                        </select>
                    </div>
                </div>
            </div>
            <div class="col-sm-5">
                <div class="form-group">
                    <label class="col-sm-4 control-label">归属部门：</label>
                    <div class="col-sm-8">
                        <input name="deptId" class="form-control" type="hidden" id="treeId" required>
                        <div class="input-group">
                            <input name="deptName" onclick="selectDeptTree()" id="treeName" type="text"
                                   placeholder="请选择归属部门" class="form-control" required>
                            <span class="input-group-addon"><i class="fa fa-search"></i></span>
                        </div>
                    </div>
                </div>
            </div>
        </div>

        <div class="row">
            <div class="col-sm-6">
                <div class="form-group">
                    <label class="col-sm-4 control-label">上传时间：</label>
                    <div class="col-sm-8 ">
                        <div class="input-group date">
                            <span class="input-group-addon"><i class="fa fa-calendar"></i></span>
                            <input id="createTime" name="createTime" class=" time-input form-control" placeholder="请选择时间" type="text" th:value="${createTime}" required>
                        </div>
                    </div>
                </div>
            </div>
        </div>

        <div class="row">
            <div class="col-sm-12">
                <div class="form-group">
                    <label class="col-sm-2 control-label">资讯内容：</label>
                    <div class="col-sm-9">
                        <input id="newContent" name="newContent" class="form-control" type="hidden">
                        <div class="summernote"></div>
                    </div>
                </div>
            </div>
        </div>

        <!--<div class="row">-->
            <!--<div class="col-sm-12">-->
                <!--<div class="form-group">-->
                    <!--<label class="col-sm-2 control-label">上传文件：</label>-->
                    <!--<div class="col-sm-9">-->
                        <!--<input id="newFile" name="newFile" class="form-control" type="hidden">-->
                        <!--<div class="file-loading">-->
                            <!--<input id="fileinput-demo-1" type="file" name="file" data-min-file-count="1" multiple>-->
                        <!--</div>-->
                    <!--</div>-->
                <!--</div>-->
            <!--</div>-->
        <!--</div>-->

        <div class="row">
            <div class="col-sm-12">
                <div class="form-group">
                    <label class="col-sm-2 control-label">上传视频：</label>
                    <div class="col-sm-9">
                        <input id="newVideo" name="newVideo" class="form-control" type="hidden">
                        <div class="file-loading">
                            <input id="fileinput-video" type="file" name="file" data-min-file-count="1"  multiple>
                        </div>
                    </div>
                </div>
            </div>
        </div>

        <div class="row">
            <div class="col-sm-12">
                <div class="form-group">
                    <label class="col-sm-2 control-label">审核人：</label>
                    <div class="col-sm-9">
                        <input name="userId" class="form-control" th:value="${userId}" type="hidden">
                        <input name="userName" class="form-control" th:value="${userName}" type="text" readonly>
                        <!--<select name="userId" class="form-control m-b" onchange="getName();" required>-->
                        <!--<option value="">请选择</option>-->
                        <!--<option th:each="user:${userList}" th:text="${user.userName}" th:value="${user.userId}"-->
                        <!--</select>-->
                    </div>
                </div>
            </div>
        </div>

        <input id="status" name="status" value="1" type="hidden">
        <input  name="type" value="1" type="hidden">
    </form>
</div>
<th:block th:include="include :: footer"/>
<th:block th:include="include :: bootstrap-fileinput-js"/>
<th:block th:include="include :: summernote-js"/>
<script type="text/javascript">
    var prefix = ctx + "module/news"
    $("#form-news-add").validate({
        focusCleanup: true
    });

    layui.use('laydate', function() {
        var laydate = layui.laydate;
        laydate.render({
            elem: '#createTime',
            type: 'datetime',
            trigger: 'click'
        });
    });

    $('.summernote').summernote({
        placeholder: '请输入内容',
        height: 200,
        lang: 'zh-CN',
        followingToolbar: false,
        callbacks: {
            onImageUpload: function (files) {
                sendFile(files[0], this);
            }
        }
    });

    // 上传文件
    function sendFile(file, obj) {
        var data = new FormData();
        data.append("file", file);
        $.ajax({
            type: "POST",
            url: ctx + "common/upload",
            data: data,
            cache: false,
            contentType: false,
            processData: false,
            dataType: 'json',
            success: function (result) {
                if (result.code == web_status.SUCCESS) {
                    $(obj).summernote('editor.insertImage', result.url, result.fileName);
                } else {
                    $.modal.alertError(result.msg);
                }
            },
            error: function (error) {
                $.modal.alertWarning("图片上传失败。");
            }
        });
    }

    //上传文件
    var uploadUrl = ctx + "common/uploadImage_QiNiu";

    $(document).ready(function () {
        $("#fileinput-demo-1").fileinput({
            'theme': 'explorer-fas',
            'uploadUrl': uploadUrl,
            overwriteInitial: false,
            allowedFileExtensions : ['jpg', 'png','gif'],//接收的文件后缀
            initialPreviewAsData: true,
            initialPreview: []
        });

        var result ="";
        // 上传成功回调
        $("#fileinput-demo-1").on("fileuploaded", function (event, data, previewId, index) {
            result += data.response.data.src+",";
            $("#newFile").val(result);
            console.log(result );
        });
    });

    var videoArr= new Array();
    var videoUrl = ctx + "common/uploadVideo_QiNiu";
    //上传视频
    $(document).ready(function () {

        $("#fileinput-video").fileinput({
            dropZoneTitle : "请上传视频！",
            'theme': 'explorer-fas',
            'uploadUrl': videoUrl,
            overwriteInitial: false,
            allowedFileExtensions : [ "mp4","avi","dat","3gp","mov","rmvb"],
            initialPreviewAsData: true,
            initialPreview: []
        });

        // 上传成功回调
        $("#fileinput-video").on("fileuploaded", function (event, data, previewId, index) {
            console.log(data);
            var videoStr = data.response.data.src;
            videoArr.push(videoStr);
            var video = videoArr.join(",");
            console.log("***"+video);
            $("#newVideo").val(video);

        });
    });


    //上传图片
    layui.use(['upload'], function () {
        var $ = layui.jquery
            , upload = layui.upload;
        //普通图片上传
        upload.render({
            elem: '#addBtn'
            , url: ctx + 'common/uploadImage_QiNiu'
            , size: 10000
            , before: function (obj) {
                //预读本地文件示例，不支持ie8
                obj.preview(function (index, file, result) {
                    $("#pic").attr("src", result);
                });
            }
            , done: function (res) {
                //如果上传失败
                if (res.code > 0) {
                    return layer.msg('上传失败');
                }
                //上传成功
                $("#newPic").val(res.data.src);
            }
        });
    });


    /*-新增-选择部门树*/
    function selectDeptTree() {
        var treeId = $("#treeId").val();
        var deptId = $.common.isEmpty(treeId) ? "100" : $("#treeId").val();
        var url = ctx + "system/dept/selectDeptTree/" + deptId;
        var options = {
            title: '选择部门',
            width: "380",
            url: url,
            callBack: doSubmit
        };
        $.modal.openOptions(options);
    }

    function doSubmit(index, layero) {
        var tree = layero.find("iframe")[0].contentWindow.$._tree;
        if ($.tree.notAllowParents(tree)) {
            var body = layer.getChildFrame('body', index);
            $("#treeId").val(body.find('#treeId').val());
            $("#treeName").val(body.find('#treeName').val());
            layer.close(index);
        }
    }

    function getName() {
        var userName = $("#userId").find("option:selected").text();
        $("#userName").val(userName);
    }

    function submitHandler() {
        if ($.validate.form()) {

            var pic =$("#newPic").val();
            if(pic == null || pic == undefined || pic == '' ){
                $.modal.alertWarning("请上传封面图！");
                return;
            }

            var sHTML = $('.summernote').summernote('code');
            $("#newContent").val(sHTML);
            $.operate.save(prefix + "/add", $('#form-news-add').serialize());
        }
    }
</script>
</body>
</html>